package com.hks360.car_treasure.activity;

import android.app.Dialog;
import android.os.Bundle;
import android.os.SystemClock;
import android.support.v4.app.FragmentTabHost;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TextView;

import com.hks360.car_treasure.R;
import com.hks360.car_treasure.application.MyApplication;
import com.hks360.car_treasure.common.AppConfig;
import com.hks360.car_treasure.common.Constant;
import com.hks360.car_treasure.common.Method;
import com.hks360.car_treasure.common.PrefKey;
import com.hks360.car_treasure.factory.FragmentFactory;
import com.hks360.car_treasure.listener.MyUpdateManagerListener;
import com.hks360.library.util.CommonUtil;
import com.hks360.library.util.PrefUtil;
import com.hks360.library.util.UIUtil;
import com.hks360.nohttp.Response;
import com.pgyersdk.Pgy;
import com.pgyersdk.update.PgyUpdateManager;
import com.umeng.analytics.MobclickAgent;

import cn.jpush.android.api.JPushInterface;


/**
 * 主页Activity
 */
public class MainActivity extends BaseActivity {
    private FragmentTabHost mTabHost;
    private String[] mTextArray;
    private LayoutInflater mLayoutInflater;
    public static boolean isForeground = false;
    private long firstClickTime = 0;
    private long secondClickTime = 0;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView();
    }

    @Override
    protected void onResume() {
        isForeground = true;
        super.onResume();
        JPushInterface.onResume(this);
    }

    @Override
    protected void onPause() {
        isForeground = false;
        super.onPause();
        JPushInterface.onPause(this);
    }

    public void initView() {
        setContentView(R.layout.layout_main);
        MyApplication.getInstance().myUpdateManagerListener = new MyUpdateManagerListener(this,MainActivity.this);
        PgyUpdateManager.register(MainActivity.this, MyApplication.getInstance().myUpdateManagerListener);
        setupView();
        initData();
    }
    public void setupView() {
        mTabHost = UIUtil.findViewById(this, R.id.main_tabhost);
    }
    public TabHost getTabHost(){
        return mTabHost;
    }
    public void initData() {
//        mLayoutInflater = LayoutInflater.from(this);
        mTextArray = getResources().getStringArray(R.array.tab_text_list);
        mTabHost.setup(this, getSupportFragmentManager(), R.id.main_real_content);
        for (int i = 0; i < mTextArray.length; i++) {
            TabHost.TabSpec tabSpec = mTabHost.newTabSpec(mTextArray[i]).setIndicator(getTabItemView(i));
            mTabHost.addTab(tabSpec, FragmentFactory.createFragment(Constant.TAG[i + 2]).getClass(), null);
        }
        mTabHost.getTabWidget().setDividerDrawable(null);
//        mTabHost.setOnTabChangedListener(new MyOnTabChangedListener());
    }
    private View getTabItemView(int index) {
        View view = View.inflate(this,R.layout.item_main_tab, null);
        ImageView imageView = UIUtil.findViewById(view, R.id.tab_imageview);
        imageView.setImageResource(Constant.TAB_IMAGE_ARRAY[index]);
        TextView textView = UIUtil.findViewById(view,R.id.tab_textview);
        textView.setText(mTextArray[index]);
        return view;
    }

//    private class MyOnTabChangedListener implements TabHost.OnTabChangeListener {
//        @Override
//        public void onTabChanged(String tabId) {
//            updateTab();
//        }
//
//    }
//
//    private void updateTab() {
//        for (int i = 0; i < mTabHost.getTabWidget().getChildCount(); i++) {
//            View view = mTabHost.getTabWidget().getChildAt(i);
//            if (mTabHost.getCurrentTab() == i) {
//                view.setBackgroundResource(R.color.white);
//            } else {
//                view.setBackgroundResource(R.color.white);
//            }
//        }
//    }

    @Override
    public void onBackPressed() {
        if (firstClickTime > 0) {
            secondClickTime = SystemClock.uptimeMillis();
            if (secondClickTime - firstClickTime < 2000) {
                AppConfig.mComManager.unLoading();
                MobclickAgent.onKillProcess(this);
                android.os.Process.killProcess(android.os.Process.myPid());
            }
            firstClickTime = 0;
            return ;
        }
        firstClickTime = SystemClock.uptimeMillis();
        CommonUtil.showToast(this, "再次点击退出程序");
        PrefUtil.putInt(this, PrefKey.COUNT, Method.getI());
    }
    @Override
    public void addListener() {

    }

    @Override
    public void viewClick(View v) {

    }
}
